package com.qdu.controller;

import com.qdu.entity.Admin;
import com.qdu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class AdminLoginController {

    @Autowired
    AdminService service;

    Admin admin = null;

    @PostMapping(value = "/vue-admin-template/user/login")
    public Map login(@RequestBody Map<String,String> params) {
        System.out.println(params);
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        System.out.println();
        admin = service.getOneByName(params.get("username"));
        if(admin != null){
            if(admin.getApwd().equals(params.get("password"))){
                responseData.put("token",1);
                response.put("code",20000);
                response.put("msg","登录成功");
                response.put("data",responseData);
                System.out.println(response);
            }else{
                // 密码错误
                response.put("code",60204);
                response.put("msg","登录失败，密码错误");
            }
        }else{
            // 没有这个管理员
            response.put("code",60000);
            response.put("msg","登录失败，没有这个管理员");
        }
        return response;
    }

    @GetMapping(value = "/vue-admin-template/user/info")
    public Map info() {
        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("roles","admin");
        responseData.put("name",admin.getAname());
        responseData.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        responseInfo.put("code",20000);
        responseInfo.put("msg","登录成功");
        responseInfo.put("data",responseData);
        return responseInfo;
    }

}
